package main

import (
	"fmt"
	"time"
)

var p = fmt.Println

func main() {
	testTime()
	testEpoch()
	testParse()
}

func testTime() {
	now := time.Now()
	p(now)
	loc, _ := time.LoadLocation("Local")
	then := time.Date(2016, 11, 17, 20, 34, 58, 651387237, loc)
	p(then)

	p(then.Year())
	p(then.Month())
	p(then.Day())
	p(then.Hour())
	p(then.Minute())
	p(then.Second())
	p(then.Nanosecond())
	p(then.Location())

	p(now.Weekday())
	p(then.Weekday())

	p(then.Before(now))
	p(then.After(now))
	p(then.Equal(now))

	diff := now.Sub(then)
	p(diff)

	p(diff.Hours())

	p(then.Add(diff))
	p(then.Add(-diff))

}

func testEpoch() {
	now := time.Now()
	secs := now.Unix()
	nanos := now.UnixNano()
	millis := nanos / 1000000

	p(now)
	p(secs)
	p(millis)
	p(nanos)

	p(time.Unix(secs, 0))
	p(time.Unix(0, nanos))
}

func testParse() {
	t := time.Now()
	p(t.Format(time.RFC3339))

	t1, _ := time.Parse(time.RFC3339, "2016-05-18T15:14:51+08:00")
	p(t1)

	// 必须使用：Mon Jan 2 15:04:05 MST 2006作为自定义格式样例
	p(t.Format("3:04PM"))
	p(t.Format("Mon Jan 2 ---- 15:04:05 2006"))

	from := "3 04 PM"
	t2, _ := time.Parse(from, "8 14 PM")
	p(t2)

	fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n", t.Year(), t.Month(), t.Day(),
		t.Hour(), t.Minute(), t.Second())

	ansic := "Mon Jan _2 15:04:05 2006"
	_, e := time.Parse(ansic, "8:41PM")
	p(e)
}
